package com.lelouch.concurrency.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Synchronized实例2
 *
 * @author lcy
 * @create 2018-05-23 10:39
 */
@Slf4j
public class SynchronizedExample2 {

    //修饰类
    public void test1(String name) {
        synchronized (SynchronizedExample2.class) {
            for (int i = 0; i < 10; i++) {
                log.info("object name:{},test1-{}",name, i);
            }
        }
    }

    //修饰静态方法
    public static synchronized void test2(String name) {
        for (int i = 0; i < 10; i++) {
            log.info("object name:{},test2-{}",name, i);
        }
    }

    public static void main(String[] args) {
        SynchronizedExample2 synchronizedExample1 = new SynchronizedExample2();
        SynchronizedExample2 synchronizedExample2 = new SynchronizedExample2();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(()->synchronizedExample1.test2("synchronizedExample1"));
        executorService.execute(()->synchronizedExample2.test2("synchronizedExample2"));
    }
}
